package model;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import domein.Mail;
import domein.Opmerking;
import domein.Programma;
import domein.Uitnodiging;

public class MailIO extends DbAbstract {

    public MailIO() {
	super();
    }

    /*
     * toevoegen van een programma
     * 
     * @author rwijhe
     */

    public void SendMail(String to, String naam, String onderwerp, String text, String aanhef) {
    		
    	   
    	   try {
    	/*Retrieve value from the text field using getParameter() method on Request object. Otherwise you can set it directly also if you are not using any interface */ 

    	        
    		   
    		   	String subject	=	onderwerp;
    	
    	         String msg		= ""; 
    	         msg = msg + text + " " + naam + ",\n \n";
    	         msg = msg + aanhef + " \n \n";
    	         
    	         final String user	= ("osinschrijving@gmail.com");
    	         final String pass	= ("themaopdracht");

    	     //create an instance of Properties Class     
    	     Properties props = new Properties();
    	     
    	/* Specifies the IP address of your default mail server
    	  for e.g if you are using gmail server as an email sever
    	  you will pass smtp.gmail.com as value of mail.smtp host. As shown here in the coding. Change accordingly, if your email id is not an gmail id*/
    	    
    	     props.put("mail.smtp.auth", "true");
    			props.put("mail.smtp.starttls.enable", "true");
    			props.put("mail.smtp.host", "smtp.gmail.com");
    			props.put("mail.smtp.port", "587");

    	     
    	 /*Pass Properties object(props) and Authenticator object   for authentication to Session instance */     

    	    Session session = Session.getInstance(props,
    	                        new javax.mail.Authenticator() {
    	  protected PasswordAuthentication getPasswordAuthentication() {
    	   return new PasswordAuthentication(user,pass);
    	   }
    	});
    	 /* Create an instance of MimeMessage, it accept MIME types and headers */    
    	 MimeMessage message = new MimeMessage(session);
    	 message.setFrom(new InternetAddress(user));
    	 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
    	 message.setSubject(subject);
    	 message.setText(msg);
    	 /* Transport class is used to deliver the message to the recipients */
    	 Transport.send(message);
    	 
    	

    	        }catch(Exception e){  
    	   e.printStackTrace();
    	  } 
    	
    }
    	public Mail getGegevens(String eventIDs){
    		int eventID = Integer.parseInt(eventIDs);
    		Mail u = new Mail("", "", "", "", "");
    		try {
    		    super.makeConnection();
    		    // Deze query gaat wat fout mee, eventID word er dubbel ingezet
    		    ResultSet rs = super.makeResultSet("SELECT emailOnderwerp, emailContent, aanhef FROM uitnodiging WHERE eventID="+eventID+"");
    		    while (rs.next()) {
    			    u.setOnderwerp(rs.getString("emailOnderwerp"));
    			    u.setText(rs.getString("emailContent"));
    			    u.setAanhef(rs.getString("aanhef"));
    			    return u;
    			}
    		    super.closeConnectRst();
    		}
    		catch(Exception e){
    			System.out.print("gout?"+e);
    		}
			return u;
    		
    	}
}
    

   
    

    

    

   

   

